package com.example.yichuangyun.mvvm.ui.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ItemAddressBookBinding;
import com.example.yichuangyun.databinding.LayoutEmptyListBinding;
import com.example.yichuangyun.model.contacts.AddressDataModel;
import com.example.yichuangyun.model.contacts.ContactsTagModel;
import com.example.yichuangyun.mvvm.ui.activity.ContactsDetailsActivity;
import com.example.yichuangyun.ui.activity.follow.AddFollowActivity;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.JumpIntent;
import com.example.yichuangyun.utils.ToastHelper;
import com.example.yichuangyun.utils.UMengUtil;

import java.util.ArrayList;
import java.util.List;

public class AddressBookAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private List<AddressDataModel.AddressModel> mData;
    private OnCallListener onCallListener;
    private int contactType = 1;

    public AddressBookAdapter(Context context, List<AddressDataModel.AddressModel> data) {
        this.context = context;
        this.mData = data;
    }

    public AddressBookAdapter(Context context, List<AddressDataModel.AddressModel> data, int contactType) {
        this.context = context;
        this.mData = data;
        this.contactType = contactType;
    }

    public void onRefresh(List<AddressDataModel.AddressModel> mList) {
        if (mList != null) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.clear();
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    public void onRefresh(List<AddressDataModel.AddressModel> mList, int contactType) {
        if (mList != null) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.clear();
            this.mData.addAll(mList);
            this.contactType = contactType;
            notifyDataSetChanged();
        }
    }

    public void addData(List<AddressDataModel.AddressModel> mList) {
        if (mList != null && mList.size() != 0) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            this.mData.addAll(mList);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //在这里根据不同的viewType进行引入不同的布局
        if (viewType == 0) {
            LayoutEmptyListBinding emptyListBinding = LayoutEmptyListBinding.inflate(LayoutInflater.from(context), parent, false);
            return new EmptyViewHolder(emptyListBinding);
        } else {
            ItemAddressBookBinding itemAddressBookBinding = ItemAddressBookBinding.inflate(LayoutInflater.from(context), parent, false);
            return new MyViewHolder(itemAddressBookBinding);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            AddressDataModel.AddressModel model = mData.get(position);
            myViewHolder.binding.tvName.setText(model.getUserName());
            if (model.getUsers() != null && model.getUsers().size() > 0) {
                if (model.getUsers().get(0).getTelNumber() != null && model.getUsers().get(0).getTelNumber().size() > 0) {
                    myViewHolder.binding.tvPhone.setText(" • " + model.getUsers().get(0).getTelNumber().get(0));
                } else if (model.getUsers().get(0).getLandLine() != null && model.getUsers().get(0).getLandLine().size() > 0) {
                    myViewHolder.binding.tvPhone.setText(" • " + model.getUsers().get(0).getLandLine().get(0));
                }
            }
            myViewHolder.binding.tvCreateName.setText(CommonUtil.checkText(model.getFoundUser()));
            myViewHolder.binding.tvCreateTime.setText(CommonUtil.checkText(model.getCreateTime()));
            myViewHolder.binding.tvFollowStatus.setText("• " + model.getFollowStatusName());
            if (contactType == 1) {
                List<ContactsTagModel> tagList = model.getLabels();
                if (tagList != null && tagList.size() > 0) {
                    myViewHolder.binding.llTag.setVisibility(View.VISIBLE);
                    final List<String> tags = new ArrayList<>();
                    for (ContactsTagModel tagModel : tagList) {
                        tags.add(tagModel.getLabelContent());
                    }
                    myViewHolder.binding.tagCloudView.setTags(tags);
                    myViewHolder.binding.tagCloudView.setOnTagClickListener(position1 -> {
                        if (position1 == -1) {
                            myViewHolder.binding.tagCloudView.singleLine(false);
                        }
                    });
                    myViewHolder.binding.tagCloudView.setOnClickListener(view -> myViewHolder.binding.tagCloudView.singleLine(true));
                } else {
                    myViewHolder.binding.llTag.setVisibility(View.GONE);
                }
            } else {
                myViewHolder.binding.llTag.setVisibility(View.GONE);
            }

            if (model.getCompanys() != null && model.getCompanys().size() > 0) {
                myViewHolder.binding.recyclerView.setAdapter(new CompanyNameAdapter(context, model.getCompanys()));
            }

            myViewHolder.binding.llCall.setOnClickListener(v -> {
                if (CommonUtil.isCanClick()) {
                    UMengUtil.setCountEventId(Constant.LIST_CALL);
                    if (onCallListener != null) {
                        List<String> mTelList = new ArrayList<>();
                        if (model.getUsers() != null && model.getUsers().size() > 0) {
                            for (AddressDataModel.UsersTelModel telModel : model.getUsers()) {
                                mTelList.addAll(telModel.getTelNumber());
                            }
                            onCallListener.callListener(mTelList, model.getId(), model.getUserName());
                        } else {
                            ToastHelper.show(context.getString(R.string.no_contact_phone));
                        }
                    } else {
                        ToastHelper.show(context.getString(R.string.no_contact_phone));
                    }
                }
            });

            myViewHolder.binding.llAddFollow.setOnClickListener(view -> {
                if (CommonUtil.isCanClick()) {
                    ArrayList<String> mTelList = new ArrayList<>();
                    if (model.getUsers() != null && model.getUsers().size() > 0) {
                        for (AddressDataModel.UsersTelModel telModel : model.getUsers()) {
                            mTelList.addAll(telModel.getTelNumber());
                        }
                    }
                    Bundle bundle = new Bundle();
                    bundle.putInt(Constant.flagKey, 1);
                    bundle.putString(Constant.businessIdKey, model.getId());
                    bundle.putString(Constant.nameKey, model.getUserName());
                    bundle.putStringArrayList(Constant.telKey, mTelList);
                    JumpIntent.jump((Activity) context, AddFollowActivity.class, bundle);
                }
            });

            myViewHolder.itemView.setOnClickListener(view -> {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.idKey, model.getId());
                bundle.putInt(Constant.contactTypeKey, contactType);
                JumpIntent.jump((Activity) context, ContactsDetailsActivity.class, bundle);
            });
            myViewHolder.binding.tagCloudView.setOnClickListener(view -> {
                Bundle bundle = new Bundle();
                bundle.putString(Constant.idKey, model.getId());
                bundle.putInt(Constant.contactTypeKey, contactType);
                JumpIntent.jump((Activity) context, ContactsDetailsActivity.class, bundle);
            });
        }
    }

    @Override
    public int getItemCount() {
        if (mData.size() == 0) {
            return 1;
        }
        //如果不为0，按正常的流程跑
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        //在这里进行判断，如果我们的集合的长度为0时，我们就使用emptyView的布局
        if (mData.size() == 0) {
            return 0;
        }
        return 1;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemAddressBookBinding binding;

        public MyViewHolder(ItemAddressBookBinding itemAddressBookBinding) {
            super(itemAddressBookBinding.getRoot());
            binding = itemAddressBookBinding;
        }
    }

    static class EmptyViewHolder extends RecyclerView.ViewHolder {

        LayoutEmptyListBinding binding;

        public EmptyViewHolder(LayoutEmptyListBinding emptyListBinding) {
            super(emptyListBinding.getRoot());
            binding = emptyListBinding;
        }
    }

    public interface OnCallListener {
        void callListener(List<String> telList, String id, String userName);
    }

    public void setOnCallListener(OnCallListener onCallListener) {
        this.onCallListener = onCallListener;
    }
}
